﻿using System;
using System.Globalization;
using Pantry.Business;
using Pantry.Entity;
using System.Web.UI;
using System.Data;

namespace Pantry.Web.Users
{
    public partial class VolunteeringOpportunityDetails : System.Web.UI.Page
    {
        private VolunteersBus _volunteersBus;
        private Volunteers _volunteers;
        private ContactBus _contactBus;

        protected void Page_Load(object sender, EventArgs e)
        {
            ListContact.SendContactToPage += delegate(string contact)
            {
                string[] contacts = contact.Split('$');
                HiddenFieldContactId.Value = contacts[0];
                TextBoxVolunteeringContact.Text = contacts[1];
            };
            if(!IsPostBack)
            {
                LoadEditData();
            }
        }

        protected void LoadEditData()
        {
            if(Request.QueryString["action"].Equals("update") && Request.QueryString["id"] != null)
            {
                try
                {
                    _volunteersBus = new VolunteersBus();
                    _contactBus = new ContactBus();
                    _volunteers = _volunteersBus.SelectVolunteersById(int.Parse(Request.QueryString["id"]));
                    TextBoxVolunteeringContact.Text = _contactBus.ManagerNameById(_volunteers.ContactId);
                    TextBoxVolunteerPorpose.Text = _volunteers.VolunteerPurpose;
                    TextBoxVolunteeringDetails.Text = _volunteers.Details;
                    TextBoxStartDate.Text = _volunteers.StartDate;
                    TextBoxEndDate.Text = _volunteers.EndDate;
                    TextBoxVoluteerNos.Text = _volunteers.VolunteerNo.ToString(CultureInfo.InvariantCulture);
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }

        protected void MarkVoluteersInActive(object sender, EventArgs e)
        {
            _volunteersBus = new VolunteersBus();
            _volunteersBus.MarkActivated(false, int.Parse(Request.QueryString["id"]));
            Response.Redirect("ViewListVoluteers.aspx");
        }

        protected void SaveServiceClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBoxStartDate.Text) && string.IsNullOrEmpty(TextBoxEndDate.Text))
            {
                var splitStart = TextBoxStartDate.Text.Split('/');
                var splitEnd = TextBoxEndDate.Text.Split('/');
                var startDate = new DateTime(int.Parse(splitStart[2]), int.Parse(splitStart[1]), int.Parse(splitStart[0]));
                var endDate = new DateTime(int.Parse(splitEnd[2]), int.Parse(splitEnd[1]), int.Parse(splitEnd[0]));
                int i = DateTime.Compare(startDate, endDate);
                if(i > 0)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "",
                                                                "showmessage('StartDate must less than EndDate !','ERROR','','false');",
                                                                true);
                    return;
                }
            }
            try
            {
                _volunteersBus = new VolunteersBus();
                _contactBus = new ContactBus();
                _volunteers = new Volunteers
                                  {
                                      ContactId = int.Parse(HiddenFieldContactId.Value),
                                      VolunteerPurpose = TextBoxVolunteerPorpose.Text,
                                      Details = TextBoxVolunteeringDetails.Text,
                                      StartDate = TextBoxStartDate.Text,
                                      EndDate = TextBoxEndDate.Text,
                                      VolunteerNo = TextBoxVoluteerNos.Text,
                                      IsActive = true
                                  };
                if(Request.QueryString["action"].Equals("create"))
                {
                    _volunteersBus.InsertVolunteers(_volunteers);
                    ScriptManager.RegisterStartupScript(this, GetType(), "",
                                                                "showmessage('Insert volunteers successful !','SUCCESS','ViewListVolunteers.aspx','false');",
                                                                true);
                }
                else
                {
                    _volunteersBus.UpdateVolunteers(_volunteers, int.Parse(Request.QueryString["id"]));
                    ScriptManager.RegisterStartupScript(this, GetType(), "",
                                                                "showmessage('Update volunteers successful !','SUCCESS','ViewListVolunteers.aspx','false');",
                                                                true);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void ButtonBackClick(object sender, EventArgs e)
        {
            Response.Redirect("ViewListVoluteers.aspx");
        }
    }
}
